#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
定义主界面

 - writer:linux_23; create: Thu Jun 05 09:28:28 2008 ; version:1; 创建
"""

import wx,os
import wx.lib.hyperlink as hl
from geosings.ui.core.MainImage import GetBitmapByName
from geosings.core.system import *
from friendpanel import *
from gispanel import *

def GetImageDirPath():
    """获取图像文件夹路径
    @rtype: str
    @return: 图像文件夹的路径
    """
    thisdir = GetThisDir(__file__)
    parentdir = os.path.split(thisdir)[0]
    return os.path.join(parentdir,"resource","images")

def GetImageByName(name, path=GetImageDirPath(), size=None):
    path = os.path.join(path,name)
    if size:
        return wx.Image(path, wx.BITMAP_TYPE_ANY ).Scale(*size)
    else:
        return wx.Image(path, wx.BITMAP_TYPE_ANY )

class DemoTaskBarIcon(wx.TaskBarIcon):
    TBMENU_CLOSE   = wx.NewId()
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame

        # Set the image
        icon = self.MakeIcon(GetImageByName('geosings.png'))
        self.SetIcon(icon, "9talk")
        self.imgidx = 1
        
        # bind some events
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)

    def CreatePopupMenu(self):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.  Just create
        the menu how you want it and return it from this function,
        the base class takes care of the rest.
        """
        menu = wx.Menu()
        menu.Append(self.TBMENU_CLOSE,   u"别来烦我")
        menu.AppendSeparator()
        return menu


    def MakeIcon(self, img):
        """
        The various platforms have different requirements for the
        icon size...
        """
        if "wxMSW" in wx.PlatformInfo:
            img = img.Scale(16, 16)
        elif "wxGTK" in wx.PlatformInfo:
            img = img.Scale(22, 22)
        # wxMac can be any size upto 128x128, so leave the source img alone....
        icon = wx.IconFromBitmap(img.ConvertToBitmap() )
        return icon
    

    def OnTaskBarActivate(self, evt):
        if self.frame.IsShown():
            self.frame.Show(False)
            return
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()

    def OnTaskBarClose(self, evt):
        wx.CallAfter(self.frame.Close)

    def OnTaskBarRemove(self, evt):
        self.RemoveIcon()

class ConfPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent, -1)
        self.SetBackgroundColour(wx.WHITE)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(sizer)  
        self._hyper = hl.HyperLinkCtrl(self, wx.ID_ANY,
                                        u"设置",
                                        URL="")
        self._hyper2 = hl.HyperLinkCtrl(self, wx.ID_ANY,
                                        u"去3snews",
                                        URL="http://www.3snews.net")
        sizer.Add((0,0), 1, wx.EXPAND, 0)
        sizer.Add(self._hyper, 0, wx.LEFT|wx.RIGHT, 3)
        sizer.Add(wx.StaticText(self,-1,"|"), 0)
        sizer.Add(self._hyper2, 0, wx.LEFT|wx.RIGHT, 3)
        self._hyper.Bind(hl.EVT_HYPERLINK_LEFT, self.OnConfigure)
        self._hyper.SetToolTip(wx.ToolTip(u"打开配置对话框"))
        self._hyper.AutoBrowse(False)
        sizer.Add((10,5))

    def OnConfigure(self,evt):
        print evt

class MePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        #self.SetSize((150, 50))
        self.SetBackgroundColour(wx.WHITE)
        imgb = GetImageByName("geosings.png",GetImageDirPath())
        #imgb.Rescale(50,50)
        bmpb = imgb.ConvertToBitmap()
        bbctrl = wx.StaticBitmap(self, -1, bmpb, (0, 0), (60, 60))
        
        #img = GetImageByName("geosings.png",GetImageDirPath())
        #img.Rescale(40,40)
        #bmp = img.ConvertToBitmap()
        #bctrl = wx.StaticBitmap(bbctrl, -1, bmp, (10, 10), (40, 40))
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(sizer)  
        sizer2 = wx.BoxSizer(wx.VERTICAL)
        sizer2.Add(wx.StaticText(self, -1, u"geosings"), 0, wx.ALL ,5)
        sizer2.Add(wx.StaticText(self, -1, u"   说的比唱的好听"), 1, wx.ALL ,5)
        sizer.Add(sizer2, 1, wx.EXPAND, 0)
        sizer.Add(bbctrl, 0, wx.ALL, 5)

def getNextImageID(count):
    imID = 0
    while True:
        yield imID
        imID += 1
        if imID == count:
            imID = 0

class MainBookPanel(wx.Toolbook):
    def __init__(self, parent):
        wx.Toolbook.__init__(self, parent, -1,
            style=wx.BK_DEFAULT|wx.BK_LEFT|wx.NO_3D|wx.NO_BORDER)
        self.SetBackgroundColour(wx.WHITE)
        il = wx.ImageList(30,54)
        bmp = GetBitmapByName("friend.png",GetImageDirPath())
        il.Add(bmp)
        self.AssignImageList(il)
        imageIdGenerator = getNextImageID(il.GetImageCount())
        self.AddPage(FriendPanel(self), '', imageId=imageIdGenerator.next())
        self.AddPage(GISDriverPanel(self), '', imageId=imageIdGenerator.next())

class MainDialog(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 
                style=wx.NO_3D  \
                        |wx.CAPTION \
                        |wx.CLOSE_BOX \
                        |wx.THICK_FRAME \
                        |wx.MINIMIZE_BOX \
                        |wx.SYSTEM_MENU
                        )
        self.SetSize((300,500))
        self.SetTitle(u" 9Talk-最好玩的GIS")

        sizer = wx.BoxSizer(wx.VERTICAL)
        hl = ConfPanel(self)
        sizer.Add(hl,0,wx.EXPAND,0)
        mp = MePanel(self)
        sizer.Add(mp,0,wx.EXPAND,0)
        bp = MainBookPanel(self)
        sizer.Add(bp,1,wx.EXPAND,0)
        #self.mainp = wx.Panel(self,-1)
        #sizer.Add(self.mainp, 1, wx.EXPAND, 0)
        #self.msizer = wx.BoxSizer(wx.VERTICAL)
        #self.mainp.SetSizer(self.msizer)
        #self.mainpanel = LoginPanel(self.mainp, self.SetMainPanel)
        #self.msizer.Add(self.mainpanel, 1, wx.EXPAND, 0)
        try:
            self.tbicon = DemoTaskBarIcon(self)
        except:
            self.tbicon = None

        
        self.SetSizer(sizer)
        self.SetAutoLayout(True)
        self.Layout()
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnCloseWindow(self, event):
        if self.tbicon is not None:
            self.tbicon.Destroy()

        self.Destroy()

    def SetMainPanel(self, panelclass):
        self.msizer.Remove(0)
        self.mainpanel.Destroy()
        self.mainpanel = panelclass(self.mainp)
        self.msizer.Add(self.mainpanel, 1, wx.EXPAND, 0)
        self.msizer.Layout()
        self.Layout()

if __name__=="__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    dlg = MainDialog()
    app.SetTopWindow(dlg)
    #dlg.ShowModal()
    dlg.Show()
    app.MainLoop()

